package service

import (
	"bloggo/structs"
	"context"
	"errors"
	"fmt"
	"gitee.com/chunanyong/logger"
	"gitee.com/chunanyong/zorm"
)

//SaveTbChatRecordStruct 保存
//如果入参ctx中没有dbConnection,使用defaultDao开启事务并最后提交
//如果入参ctx有dbConnection且没有事务,调用dbConnection.begin()开启事务并最后提交
//如果入参ctx有dbConnection且有事务,只使用不提交,有开启方提交事务
//但是如果遇到错误或者异常,虽然不是事务的开启方,也会回滚事务,让事务尽早回滚
func SaveTbChatRecordStruct(ctx context.Context, tbchatrecordstruct *structs.TbChatRecordStruct) error {

	// tbchatrecordstruct对象指针不能为空
	if tbchatrecordstruct == nil {
		return errors.New("tbchatrecordstruct对象指针不能为空")
	}
	//匿名函数return的error如果不为nil,事务就会回滚
	_, errSaveTbChatRecordStruct := zorm.Transaction(ctx, func(ctx context.Context) (interface{}, error) {

		//事务下的业务代码开始

		_, errSaveTbChatRecordStruct := zorm.Insert(ctx, tbchatrecordstruct)

		if errSaveTbChatRecordStruct != nil {
			return nil, errSaveTbChatRecordStruct
		}

		return nil, nil
		//事务下的业务代码结束

	})

	//记录错误
	if errSaveTbChatRecordStruct != nil {
		errSaveTbChatRecordStruct := fmt.Errorf("service.SaveTbChatRecordStruct错误:%w", errSaveTbChatRecordStruct)
		logger.Error(errSaveTbChatRecordStruct)
		return errSaveTbChatRecordStruct
	}

	return nil
}

//UpdateTbChatRecordStruct 更新
//如果入参ctx中没有dbConnection,使用defaultDao开启事务并最后提交
//如果入参ctx有dbConnection且没有事务,调用dbConnection.begin()开启事务并最后提交
//如果入参ctx有dbConnection且有事务,只使用不提交,有开启方提交事务
//但是如果遇到错误或者异常,虽然不是事务的开启方,也会回滚事务,让事务尽早回滚
func UpdateTbChatRecordStruct(ctx context.Context, tbchatrecordstruct *structs.TbChatRecordStruct) error {

	// tbchatrecordstruct对象指针或主键Id不能为空
	if tbchatrecordstruct == nil || (tbchatrecordstruct.Id) < 1 {
		return errors.New("tbchatrecordstruct对象指针或主键Id不能为空")
	}

	//匿名函数return的error如果不为nil,事务就会回滚
	_, errUpdateTbChatRecordStruct := zorm.Transaction(ctx, func(ctx context.Context) (interface{}, error) {

		//事务下的业务代码开始
		_, errUpdateTbChatRecordStruct := zorm.Update(ctx, tbchatrecordstruct)

		if errUpdateTbChatRecordStruct != nil {
			return nil, errUpdateTbChatRecordStruct
		}

		return nil, nil
		//事务下的业务代码结束

	})

	//记录错误
	if errUpdateTbChatRecordStruct != nil {
		errUpdateTbChatRecordStruct := fmt.Errorf("service.UpdateTbChatRecordStruct错误:%w", errUpdateTbChatRecordStruct)
		logger.Error(errUpdateTbChatRecordStruct)
		return errUpdateTbChatRecordStruct
	}

	return nil
}

//UpdateNotZeroTbChatRecordStruct 更新
//如果入参ctx中没有dbConnection,使用defaultDao开启事务并最后提交
//如果入参ctx有dbConnection且没有事务,调用dbConnection.begin()开启事务并最后提交
//如果入参ctx有dbConnection且有事务,只使用不提交,有开启方提交事务
//但是如果遇到错误或者异常,虽然不是事务的开启方,也会回滚事务,让事务尽早回滚
func UpdateNotZeroTbChatRecordStruct(ctx context.Context, tbchatrecordstruct *structs.TbChatRecordStruct) error {

	// tbchatrecordstruct对象指针或主键Id不能为空
	if tbchatrecordstruct == nil || (tbchatrecordstruct.Id) < 1 {
		return errors.New("tbchatrecordstruct对象指针或主键Id不能为空")
	}

	//匿名函数return的error如果不为nil,事务就会回滚
	_, errUpdateTbChatRecordStruct := zorm.Transaction(ctx, func(ctx context.Context) (interface{}, error) {

		//事务下的业务代码开始
		_, errUpdateTbChatRecordStruct := zorm.UpdateNotZeroValue(ctx, tbchatrecordstruct)

		if errUpdateTbChatRecordStruct != nil {
			return nil, errUpdateTbChatRecordStruct
		}

		return nil, nil
		//事务下的业务代码结束

	})

	//记录错误
	if errUpdateTbChatRecordStruct != nil {
		errUpdateTbChatRecordStruct := fmt.Errorf("service.UpdateNotZeroTbChatRecordStruct错误:%w", errUpdateTbChatRecordStruct)
		logger.Error(errUpdateTbChatRecordStruct)
		return errUpdateTbChatRecordStruct
	}

	return nil
}

//DeleteTbChatRecordStructById 根据Id删除
//如果入参ctx中没有dbConnection,使用defaultDao开启事务并最后提交
//如果入参ctx有dbConnection且没有事务,调用dbConnection.begin()开启事务并最后提交
//如果入参ctx有dbConnection且有事务,只使用不提交,有开启方提交事务
//但是如果遇到错误或者异常,虽然不是事务的开启方,也会回滚事务,让事务尽早回滚
func DeleteTbChatRecordStructById(ctx context.Context, id string) error {

	//id不能为空
	if len(id) < 1 {
		return errors.New("id不能为空")
	}

	//匿名函数return的error如果不为nil,事务就会回滚
	_, errDeleteTbChatRecordStruct := zorm.Transaction(ctx, func(ctx context.Context) (interface{}, error) {

		//事务下的业务代码开始
		finder := zorm.NewDeleteFinder(structs.TbChatRecordStructTableName).Append(" WHERE id=?", id)
		_, errDeleteTbChatRecordStruct := zorm.UpdateFinder(ctx, finder)

		if errDeleteTbChatRecordStruct != nil {
			return nil, errDeleteTbChatRecordStruct
		}

		return nil, nil
		//事务下的业务代码结束

	})

	//记录错误
	if errDeleteTbChatRecordStruct != nil {
		errDeleteTbChatRecordStruct := fmt.Errorf("service.DeleteTbChatRecordStruct错误:%w", errDeleteTbChatRecordStruct)
		logger.Error(errDeleteTbChatRecordStruct)
		return errDeleteTbChatRecordStruct
	}

	return nil
}

//FindTbChatRecordStructById 根据Id查询信息
//ctx中如果没有dbConnection,则会使用默认的datasource进行无事务查询
func FindTbChatRecordStructById(ctx context.Context, id string) (*structs.TbChatRecordStruct, error) {
	//id不能为空
	if len(id) < 1 {
		return nil, errors.New("id不能为空")
	}

	//根据Id查询
	finder := zorm.NewSelectFinder(structs.TbChatRecordStructTableName).Append(" WHERE id=?", id)
	tbchatrecordstruct := structs.TbChatRecordStruct{}
	has, errFindTbChatRecordStructById := zorm.QueryRow(ctx, finder, &tbchatrecordstruct)

	//记录错误
	if errFindTbChatRecordStructById != nil {
		errFindTbChatRecordStructById := fmt.Errorf("service.FindTbChatRecordStructById错误:%w", errFindTbChatRecordStructById)
		logger.Error(errFindTbChatRecordStructById)
		return nil, errFindTbChatRecordStructById
	}

	if !has {
		return nil, nil
	}

	return &tbchatrecordstruct, nil

}

//FindTbChatRecordStructList 根据Finder查询列表
//ctx中如果没有dbConnection,则会使用默认的datasource进行无事务查询
func FindTbChatRecordStructList(ctx context.Context, finder *zorm.Finder, page *zorm.Page) ([]structs.TbChatRecordStruct, error) {

	//finder不能为空
	if finder == nil {
		return nil, errors.New("finder不能为空")
	}

	tbchatrecordstructList := make([]structs.TbChatRecordStruct, 0)
	errFindTbChatRecordStructList := zorm.Query(ctx, finder, &tbchatrecordstructList, page)

	//记录错误
	if errFindTbChatRecordStructList != nil {
		errFindTbChatRecordStructList := fmt.Errorf("service.FindTbChatRecordStructList错误:%w", errFindTbChatRecordStructList)
		logger.Error(errFindTbChatRecordStructList)
		return nil, errFindTbChatRecordStructList
	}

	return tbchatrecordstructList, nil
}
